﻿using Microsoft.Win32;
using MkLink;

namespace MklinkRunas
{
    internal class Program
    {
        private static void AddregInfo(string ExePate)
        {

            using (RegistryKey CRoot = Registry.ClassesRoot)
            {
                RegistryKey? Project = null, command = null, Project_D = null, command_D = null;
                try
                {
                    Project = CRoot.OpenSubKey("*\\shell\\SymbolicLinkManage", true);
                    if (Project == null)
                        Project = CRoot.CreateSubKey("*\\shell\\SymbolicLinkManage");

                    Project.SetValue(string.Empty, $"新增符号链接到->");
                    Project.SetValue("Icon", $"\"{ExePate}\"");

                    command = Project.OpenSubKey("command", true);
                    if (command == null)
                        command = Project.CreateSubKey("command", true);

                    command.SetValue(string.Empty, $"\"{ExePate}\" \"%1\"");


                    Project_D = CRoot.OpenSubKey("Directory\\shell\\SymbolicLinkManage", true);
                    if (Project_D == null)
                        Project_D = CRoot.CreateSubKey("Directory\\shell\\SymbolicLinkManage");

                    Project_D.SetValue(string.Empty, $"新增符号链接到->");
                    Project_D.SetValue("Icon", $"\"{ExePate}\"");

                    command_D = Project_D.OpenSubKey("command", true);
                    if (command_D == null)
                        command_D = Project_D.CreateSubKey("command", true);

                    command_D.SetValue(string.Empty, $"\"{ExePate}\" \"%1\"");

                }
                catch (Exception exc)
                {
                    Console.WriteLine($"注册系统右键菜单,注册失败：{exc.Message}");
                    Environment.ExitCode = -6;
                }
                finally
                {
                    Project?.Close();
                    command?.Close();
                }
            }
        }
        static void Main(string[] args)
        {
            if(args.Length == 2 && args[0].Equals("AddregInfo",StringComparison.OrdinalIgnoreCase))
            {
                AddregInfo(args[1]);
                return;
            }
            
            if (args.Length < 3)
            {
                Console.Error.WriteLine("缺少参数");
                Environment.ExitCode = -1;
                return;
            }
            if (!Path.IsPathRooted(args[0]))
            {
                Console.Error.WriteLine($"LinkPointPath:[{args[0]}] 路径错误");
                Environment.ExitCode = -2;
                return;
            }
            if (!Path.Exists(args[1]))
            {
                Console.Error.WriteLine($"LinkTargetPath:[{args[1]}] 路径错误");
                Environment.ExitCode = -3;
                return;
            }
            LinkType linkType;
            if (int.TryParse(args[2], out int lt))
                linkType = (LinkType)lt;
            else if (!Enum.TryParse<LinkType>(args[2], out linkType))
            {
                Console.Error.WriteLine($"LinkType:[{args[2]}] 连接类型参数错误");
                Environment.ExitCode = -4;
                return;
            }
            try
            {
                MkLinkTools.CreateLink(linkType, args[0], args[1]);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"创建连接错误【{ex.Message}】");
                Environment.ExitCode = -5;
            }
            Console.WriteLine($"[{linkType}]：{args[0]}<======>{args[1]} 创建成功!");
            Environment.ExitCode = 0;
        }
    }
}
